<?php

class Slideshow {

    private $_ID;
    private $_title;
    private $_image;
    private $_createdDate;
    private $_modifiedDate;
    private $_createdUserID;
    private $_modifiedUserID;
    private $_status;
    private $_order;
    private $_link;
    private $_createdUser;
    private $_modifiedUser;

    public function getID() {
        return $this->_ID;
    }

    public function setID($ID) {
        $this->_ID = $ID;
    }

    public function getTitle() {
        return $this->_title;
    }

    public function setTitle($title) {
        $this->_title = $title;
    }

    public function getImage() {
        return $this->_image;
    }

    public function setImage($image) {
        $this->_image = $image;
    }

    public function getCreatedDate() {
        return $this->_createdDate;
    }

    public function setCreatedDate($createdDate) {
        $this->_createdDate = $createdDate;
    }

    public function getModifiedDate() {
        return $this->_modifiedDate;
    }

    public function setModifiedDate($modifiedDate) {
        $this->_modifiedDate = $modifiedDate;
    }

    public function getCreatedUserID() {
        return $this->_createdUserID;
    }

    public function setCreatedUserID($createdUserID) {
        $this->_createdUserID = $createdUserID;
    }

    public function getModifiedUserID() {
        return $this->_modifiedUserID;
    }

    public function setModifiedUserID($modifiedUserID) {
        $this->_modifiedUserID = $modifiedUserID;
    }

    public function getStatus() {
        return $this->_status;
    }

    public function setStatus($status) {
        $this->_status = $status;
    }

    public function getOrder() {
        return $this->_order;
    }

    public function setOrder($order) {
        $this->_order = $order;
    }

    public function getLink() {
        return $this->_link;
    }

    public function setLink($link) {
        $this->_link = $link;
    }

    public function getCreatedUser() {
        return $this->_createdUser;
    }

    public function setCreatedUser($createdUser) {
        $this->_createdUser = $createdUser;
    }

    public function getModifiedUser() {
        return $this->_modifiedUser;
    }

    public function setModifiedUser($modifiedUser) {
        $this->_modifiedUser = $modifiedUser;
    }

    public function __construct() {
        $this->_ID = null;
        $this->_title = null;
        $this->_image = null;
        $this->_createdDate = null;
        $this->_modifiedDate = null;
        $this->_createdUserID = null;
        $this->_modifiedUserID = null;
        $this->_status = null;
        $this->_order = null;
        $this->_link = null;
        $this->_createdUser = null;
        $this->_modifiedUser = null;
    }

    private function setSlideshow($object, $row) {
        $object->setID($row->SS_ID);
        $object->setTitle($row->SS_Title);
        $object->setImage($row->SS_Image);
        $object->setCreatedDate($row->SS_CreatedDate);
        $object->setStatus($row->SS_Status);
        $object->setOrder($row->SS_Order);
        $object->setLink($row->SS_Link);
        $object->setModifiedDate($row->SS_ModifiedDate);
        $object->setCreatedUserID($row->SS_CreatedUser);
        $object->setModifiedUserID($row->SS_ModifiedUser);
        return $object;
    }

    public function add($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `slideshow`
                                (
                                 `SS_Title`,
                                 `SS_Image`,
                                 `SS_CreatedDate`,
                                 `SS_Status`,
                                 `SS_Order`,
                                 `SS_Link`,
                                 `SS_ModifiedDate`,
                                 `SS_CreatedUser`,
                                 `SS_ModifiedUser`)
                    VALUES (
                            :SS_Title,
                            :SS_Image,
                            :SS_CreatedDate,
                            :SS_Status,
                            :SS_Order,
                            :SS_Link,
                            :SS_ModifiedDate,
                            :SS_CreatedUser,
                            :SS_ModifiedUser);";

            $parameters = array(
                ':SS_Title', ':SS_Image',
                ':SS_CreatedDate', ':SS_Status',
                ':SS_Order', ':SS_Link',
                ':SS_ModifiedDate', ':SS_CreatedUser',
                ':SS_ModifiedUser'
            );

            $values = array(
                $this->getTitle(), $this->getImage(),
                $this->getCreatedDate(), $this->getStatus(),
                $this->getOrder(), $this->getLink(),
                $this->getModifiedDate(), $this->getCreatedUserID(),
                $this->getModifiedUserID()
            );

            $types = array(
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_INT,
                PDO::PARAM_INT, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_INT,
                PDO::PARAM_INT
            );
            return $db->executeNonQuery($sql, $parameters, $values, $types, true);
        }
    }

    public function delete($db = null) {
        if ($db != null) {
            $sql = "DELETE
                    FROM `slideshow`
                    WHERE `SS_ID` = :SS_ID;";

            $parameters = array(':SS_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function update($db = null) {
        if ($db != null) {
            $sql = "UPDATE `slideshow`
                    SET 
                      `SS_Title` = :SS_Title,
                      `SS_Image` = :SS_Image,
                      `SS_Status` = :SS_Status,
                      `SS_Link` = :SS_Link,
                      `SS_ModifiedDate` = :SS_ModifiedDate,
                      `SS_ModifiedUser` = :SS_ModifiedUser
                    WHERE `SS_ID` = :SS_ID;";

            $parameters = array(
                ':SS_Title', ':SS_Image',
                ':SS_Status',
                ':SS_Link',
                ':SS_ModifiedDate',
                ':SS_ModifiedUser', ':SS_ID'
            );

            $values = array(
                $this->getTitle(), $this->getImage(),
                $this->getStatus(),
                $this->getLink(),
                $this->getModifiedDate(),
                $this->getModifiedUserID(), $this->getID()
            );

            $types = array(
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_INT, PDO::PARAM_INT
            );
            return $db->executeNonQuery($sql, $parameters, $values, $types, true);
        }
    }

    public function getList($db = null) {
        if ($db != null) {
            $sqlQuery = "SELECT
                        `SS_ID`,
                        `SS_Title`,
                        `SS_Image`,
                        `SS_CreatedDate`,
                        `SS_Status`,
                        `SS_Order`,
                        `SS_Link`,
                        `SS_ModifiedDate`,
                        `SS_CreatedUser`,
                        `SS_ModifiedUser`
                      FROM `slideshow`
                      ORDER BY SS_Order DESC;";
            $parameters = null;
            $values = null;
            $types = null;
            $result = $db->executeQuery($sqlQuery, $parameters, $values, $types);

            $rows = array();
            if ($result != null) {

                while ($row = $result->fetch()) {

                    $slideshow = new Slideshow();
                    $slideshow = $this->setSlideshow($slideshow, $row);


                    $createdUser = new Users();
                    $createdUser->setID($slideshow->getCreatedUserID());
                    $createdUser = $createdUser->getByID($db);
                    $slideshow->setCreatedUser($createdUser);

                    $modifiedUser = new Users();
                    $modifiedUser->setID($slideshow->getModifiedUserID());
                    $modifiedUser = $modifiedUser->getByID($db);
                    $slideshow->setModifiedUser($modifiedUser);

                    $rows[] = $slideshow;
                }
            }
            return $rows;
        }
    }

    public function getByID($db = null) {
        if ($db != null) {
            $sqlQuery = "SELECT
                        `SS_ID`,
                        `SS_Title`,
                        `SS_Image`,
                        `SS_CreatedDate`,
                        `SS_Status`,
                        `SS_Order`,
                        `SS_Link`,
                        `SS_ModifiedDate`,
                        `SS_CreatedUser`,
                        `SS_ModifiedUser`
                      FROM `slideshow`
                      WHERE SS_ID = :SS_ID";
            $parameters = array(':SS_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            $result = $db->executeQuery($sqlQuery, $parameters, $values, $types);

            $rows = array();
            if ($result != null) {
                $row = $result->fetch();

                $createdUser = new Users();
                $createdUser->setID($this->getCreatedUserID());
                $createdUser = $createdUser->getByID($db);
                $this->setCreatedUser($createdUser);

                $modifiedUser = new Users();
                $modifiedUser->setID($this->getModifiedUserID());
                $modifiedUser = $modifiedUser->getByID($db);
                $this->setModifiedUser($modifiedUser);

                return $this->setSlideshow($this, $row);
            } else {
                return null;
            }
        }
    }

    public function getMaxOrder($db) {
        if ($db != null) {
            $sql = "SELECT MAX(SS_Order) + 1 AS MaxOrder FROM slideshow";
            $parameters = null;
            $values = null;
            $types = null;
            $result = $db->executeQuery($sql, $parameters, $values, $types);

            $row = $result->fetch();
            if ($row->MaxOrder != null) {
                return $row->MaxOrder;
            } else {
                return 1;
            }
        }
    }

    public function orderUp($db) {
        if ($db != null) {
            // câu lệnh tải currentOrder, nextOrder, nextArticleID
            $sql = "SELECT (SELECT `SS_Order` 
                    from slideshow 
                    WHERE SS_ID = :SS_ID) As `currenOrder`,               
                
                    (SELECT `SS_Order` from slideshow WHERE `SS_Order` > 
                    (SELECT `SS_Order` from slideshow WHERE SS_ID = :SS_ID) 
                    Order by `SS_Order` LIMIT 1) As `nextOrder`,

                    (SELECT SS_ID from slideshow WHERE `SS_Order` = 
                    (SELECT `SS_Order` from slideshow WHERE `SS_Order` > 
                    (SELECT `SS_Order` from slideshow WHERE SS_ID = :SS_ID) 
                    Order by `SS_Order` LIMIT 1)) As `nextID`";
            $parameters = array(':SS_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            $result = $db->executeQuery($sql, $parameters, $values, $types);



            if ($result != null) {
                $row = $result->fetch();
                // cập nhật order của previous article bằng với currentOrder
                $sqlNextArticle = "update slideshow set `SS_Order` = :SS_Order where `SS_ID` = :SS_ID";
                $parameters2 = array(':SS_Order', ':SS_ID');
                $values2 = array($row->currenOrder, $row->nextID);
                $types2 = array(PDO::PARAM_INT, PDO::PARAM_INT);

                $db->executeNonQuery($sqlNextArticle, $parameters2, $values2, $types2);

                // cập nhật order của current article bằng với previous order
                $sqlCurrentArticle = "update slideshow set `SS_Order` = :SS_Order where `SS_ID` = :SS_ID";
                $parameters3 = array(':SS_Order', ':SS_ID');
                $values3 = array($row->nextOrder, $this->getID());
                $types3 = array(PDO::PARAM_INT, PDO::PARAM_INT);
                $db->executeNonQuery($sqlCurrentArticle, $parameters3, $values3, $types3);
            }
        }
    }

    function orderDown($db) {
        if ($db != null) {
            // câu lệnh tải currentOrder, previousOrder, previousArticleID
            $sql = "SELECT (SELECT `SS_Order` from slideshow 
                WHERE SS_ID = :SS_ID) As `currenOrder`, 
                    
		(SELECT `SS_Order` from slideshow WHERE `SS_Order` < 
		(SELECT `SS_Order` from slideshow WHERE SS_ID = :SS_ID) 
		Order by `SS_Order` desc LIMIT 1) As `previousOrder`,
                
		(SELECT SS_ID from slideshow WHERE `SS_Order` = 
		(SELECT `SS_Order` from slideshow WHERE `SS_Order` < 
		(SELECT `SS_Order` from slideshow WHERE SS_ID = :SS_ID) 
		Order by `SS_Order` desc LIMIT 1)) As `previousID`";

            $parameters = array(':SS_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            if ($result != null) {

                $row = $result->fetch();

                // cập nhật order của previous bằng với currentOrder
                $sqlPreviousArticle = "update slideshow set `SS_Order` = :SS_Order where `SS_ID` = :SS_ID";
                $parameters2 = array(':SS_Order', ':SS_ID');
                $values2 = array($row->currenOrder, $row->previousID);
                $types2 = array(PDO::PARAM_INT, PDO::PARAM_INT);
                $db->executeNonQuery($sqlPreviousArticle, $parameters2, $values2, $types2);

                // cập nhật order của current article bằng với previous order
                $sqlCurrentArticle = "update slideshow set `SS_Order` = :SS_Order where `SS_ID` = :SS_ID";
                $parameters3 = array(':SS_Order', ':SS_ID');
                $values3 = array($row->previousOrder, $this->getID());
                $types3 = array(PDO::PARAM_INT, PDO::PARAM_INT);
                $db->executeNonQuery($sqlCurrentArticle, $parameters3, $values3, $types3);
            }
        }
    }

    public function updateStatus($db = null) {
        if ($db != null) {
            $sql = "UPDATE `slideshow`
                    SET `SS_Status` = :SS_Status
                    WHERE `SS_ID` = :SS_ID;";

            $parameters = array(':SS_Status', ':SS_ID');
            $values = array($this->getStatus(), $this->getID());
            $types = array(PDO::PARAM_INT, PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

}

?>
